var Request = require( '../request' );
const $request = new Request();
const config = require( '../../config' );
const multipart = require( 'connect-multiparty' )
const multipartMiddleware = multipart()
const axios = require( 'axios' )

class commonModule {
	/**
	 * 功能描述
	 * @param {*} params
	 * @param {*} res
	 * @param {*} req
	 */
	function1( params, res, req ) {
		res.json( params );
		// let apiUrl = config.serverUrl + "/apiUri";
		// $request.handleRequest({
		//   method: 'post',
		//   params: params,
		//   actionUrl: apiUrl,
		// }, res);
	}

	createResource( params, res, req ) {
		let insertUrl = config.serverUrl_BI + "/bi/etl/createResource";
		let updateUrl = config.serverUrl_BI + "/bi/etl/updateResource"
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: params.data.id ? updateUrl : insertUrl,
		}, res );
	}

	getResources( params, res, req ) {
		let apiUrl = config.serverUrl_BI + "/bi/etl/getResources";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	delResource( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/delResource";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	getModelPage( params, res, req ) {
		let apiUrl = config.serverUrl_BI + "/biEtlModel/getModelPage";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	createModel( params, res, req ) {
		params.data = params.data.data;
		let insertUrl = config.serverUrl_BI + "/biEtlModel/createModel";
		let updateUrl = config.serverUrl_BI + "/biEtlModel/updateModel";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: params.data.id ? updateUrl : insertUrl,
		}, res );
	}

	delModel( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/biEtlModel/delModel";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	activeModel( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/biEtlModel/runProcesGroup";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	// biUploadFile(params, res, req){
	//   console.log(params)
	//   // console.log(params)
	//   // console.log(req.files)
	//   // params.data = params.data.data;
	//   let apiUrl = config.serverUrl_BI + "/biEtlDbFile/upload";
	//   // $request.uploadFile({
	//   //   method: 'post',
	//   //   params: params,
	//   //   actionUrl: apiUrl,
	//   // }, res);
	// }

	delFileBI( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/biEtlDbFile/delete";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	runResource( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/runResource";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}


	createFileResource( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/createFileResource";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
			timeout: 60000,
		}, res );
		// axios.post().then(function (response) {
		//   res.send(JSON.stringify(response.data));
		// }).catch(
		//   function (error) {
		//     console.log(error);
		//   }
		// )
	}

	appendFileResource( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/appendFileResource";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
			timeout: 60000,
		}, res );
	}

	resetFileResource( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/resetFileResource";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
			timeout: 60000,
		}, res );
	}

//  测试连接
	testConnection( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/testConnection";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}


//**********数据预览****************//

	getTables( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/getTables";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	getTableSchema( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/getTableSchema";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	getTableData( params, res, req ) {
		params.data = params.data.data;
		let apiUrl = config.serverUrl_BI + "/bi/etl/getTableData";
		$request.handleRequest( {
			method: 'post',
			params: params,
			actionUrl: apiUrl,
		}, res );
	}

	getTopMenu( params ) {
		return new Promise( ( resolve, reject ) => {
			$request.doAction( {
				method: 'post',
				params: params,
				actionUrl: config.serverUrl + "/resources",
			} ).then( ( body ) => {
				resolve( body );
			} ).catch( ( err ) => {
				reject( err );
			} );
		} )
	}

	/**
	 * 初始化数据组装
	 * @param {{}} params
	 * @param {{}}} res
	 */
	async getInitData( params, res ) {
		let $body = { user: params.user };
		params.data.userId = params.operator;
		//	获取主系统顶部模块id
		const topMenusInfo = await this.getTopMenu( params ).catch( err => err );

		let systemId = '';
		//	找到bi模块的id
		if ( topMenusInfo.success ) {
			const biItem = topMenusInfo.data.find( v => v.permittedAction === 'newbi' );

			if ( biItem ) {
				systemId = biItem.systemId;
			}
		}

		if ( !systemId ) res.json( { status: -1, err: topMenusInfo.message } );

		//	获取bi模块的菜单
		params.data.systemId = systemId;
		const leftMenuInfo = await this.getTopMenu( params ).catch( err => err );

		if ( leftMenuInfo.success ) {
			$body.menu = leftMenuInfo.data[0].childrenList;
			$body.biHelpDocInfo = {
				systemId: $body.menu[0].systemId
			};
		}

		//	获取bi的按钮权限
		params.data.systemId = 'button';
		const btnAuth = await this.getTopMenu( params ).catch( err => err );

		if ( btnAuth.success ) {
			$body.btns = btnAuth.data;
		}

		//类型转换(java端存储redis方式为数据结构，值会转换为string，前端获取redis缓存user信息后将部分原始类型为数值型从string转换为数值)
		let intArr = [ "userId", "tenantId", "organizationId", "userBankId", "employeeId",
			"deptId", "empDeptLeader", "empLoanLimitAmount", "empSupervisorEmpId", "empPositionId",
			"internalTenantFlag", "tenantActiveFlag" ];
		Object.keys( $body.user ).map( key => {
			if ( intArr.indexOf( key ) >= 0 && $body.user[ key ] ) {
				$body.user[ key ] = parseInt( $body.user[ key ] );
			}
		} );

		res.json( { status: 1, data: $body } );
	}
}

module.exports = commonModule;
